package com.itmuch.cloud.study.user.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.itmuch.cloud.study.user.entity.User;
import com.itmuch.cloud.study.user.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class MovieController
{
    protected static final Logger logger = LoggerFactory.getLogger(MovieController.class);

    @Autowired
    private UserService userService;

    /**
     * 容错指定回退方法
     * 
     * @param id
     * @return
     * @see [类、类#方法、类#成员]
     */
    @HystrixCommand(fallbackMethod = "findByIdFallback")
    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id)
    {
        return this.userService.findById(id);
    }

    /**
     * 添加Throwable 后，不会触发回退，但会打印异常信息
     *
     * @param id
     * @param throwable
     * @return
     * @see [类、类#方法、类#成员]
     */
    public User findByIdFallback(Long id, Throwable throwable)
    {
        User user = new User();
        user.setId(-1L);
        user.setName("the default user");
        return user;
    }
}
